package cn.onekit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSON {

	/**
	 * 序列化
	 * 
	 */
	public static JSONObject parse(Object string) {
		if (string == null) {
			return null;
		}
		try {
			return new JSONObject((String) string);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 序列化
	 */
	public static JSONArray parses(Object string) {
		try {
			return new JSONArray((String) string);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 反序列化
	 */
	public static String stringify(JSONObject json) {
		try {
			return json.toString();
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 反序列化
	 */
	public static String stringify(JSONArray json) {
		try {
			return json.toString();
		} catch (Exception e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public static <T> List<T> toList(JSONArray json) {
		List<T> RESULT = new ArrayList<T>();
		for (int i = 0; i < json.length(); i++) {
			Object item = json.opt(i);
			RESULT.add((T) item);
		}
		return RESULT;
	}

	public static Map<String, Object> toMap(JSONObject json) {
		Map<String, Object> RESULT = new HashMap<String, Object>();
		Iterator<String> keys = json.keys();
		while (keys.hasNext()) {
			String key = keys.next();
			Object item = json.opt(key);
			RESULT.put(key, item);
		}
		return RESULT;
	}

	public static JSONArray fromList(@SuppressWarnings("rawtypes") List list) {
		JSONArray RESULT = new JSONArray();
		for (Object item : list) {
			RESULT.put(item);
		}
		return RESULT;
	}

	public static JSONObject fromMap(Map<String, Object> map) {
		JSONObject RESULT = new JSONObject();
		for (String key : map.keySet()) {
			Object item = map.get(key);
			try {
				RESULT.put(key, item);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return RESULT;
	}

	public static Object find(JSONArray array, Object value) {
		try {
			for (int i = 0; i < array.length(); i++) {
				Object item = array.get(i);
				if (item.toString().equals(value.toString())) {
					return item;
				}
			}
			return null;
		} catch (Exception e) {
			return null;
		}
	}

	public static boolean contains(JSONArray array, Object value) {
		return find(array, value) != null;
	}

	/*
	 * public static Date toDate(String str) { String str1 = "/Date("; String
	 * str2 = ")/"; str = str.substring(str1.length(), str.length() -
	 * str2.length()); int p = str.indexOf('+'); if (p >= 0) { str =
	 * str.substring(0, p); } Date date = new Date(Long.valueOf(str)); return
	 * date; }
	 * 
	 * @SuppressWarnings("unchecked") public static JSONArray remove(int index,
	 * JSONArray jsonarray) { if (index < 0) return jsonarray; Field valuesField
	 * = null; try { valuesField = JSONArray.class.getDeclaredField("values");
	 * valuesField.setAccessible(true); List<Object> values = (List<Object>)
	 * valuesField.get(jsonarray); if (index >= values.size()) return jsonarray;
	 * values.remove(index); } catch (NoSuchFieldException e) {
	 * e.printStackTrace(); return jsonarray; } catch (IllegalAccessException e)
	 * { e.printStackTrace(); return jsonarray; } catch
	 * (IllegalArgumentException e) { e.printStackTrace(); return jsonarray; }
	 * 
	 * return jsonarray; }
	 */
	public static JSONArray remove(JSONArray array, Object item) {
		if (array == null) {
			return null;
		}
		try {
			JSONArray RESULT = new JSONArray();
			for (int i = 0; i < array.length(); i++) {
				Object itm = array.get(i);
				if (!item.equals(itm)) {
					RESULT.put(itm);
				}
			}
			return RESULT;
		} catch (Exception e) {
			return array;
		}
	}

}
